package com.personal.gateway.flinkAllCommand;

import com.alibaba.fastjson.JSONObject;
import com.personal.gateway.flinkAllCommand.configurationOption.ConfigurationOption;
import com.personal.gateway.flinkAllCommand.configurationOption.ConfigurationOptionBuilder;
import com.personal.gateway.flinkAllCommand.configurationOption.KeyIndexInfomationDao;
import com.personal.gateway.flinkAllCommand.configurationOption.configurationGenerator.ConfigurationGeneratorDouble;
import com.personal.gateway.flinkAllCommand.configurationOption.configurationGenerator.ConfigurationGeneratorEnum;
import com.personal.gateway.flinkAllCommand.configurationOption.configurationGenerator.ConfigurationGeneratorInteger;
import com.personal.gateway.flinkAllCommand.configurationOption.configurationGenerator.ConfigurationGeneratorListMap;
import com.personal.gateway.flinkAllCommand.configurationOption.configurationType.*;
import com.personal.gateway.flinkAllCommand.configurationOption.parameterCheck.ParameterCheckDouble;
import com.personal.gateway.flinkAllCommand.configurationOption.parameterCheck.ParameterCheckEnum;
import com.personal.gateway.flinkAllCommand.configurationOption.parameterCheck.ParameterCheckInteger;
import com.personal.gateway.flinkAllCommand.configurationOption.parameterCheck.ParameterCheckListMap;
import com.personal.gateway.flinkAllCommand.configurationOption.webObjectGenerator.WebObjectGeneratorDouble;
import com.personal.gateway.flinkAllCommand.configurationOption.webObjectGenerator.WebObjectGeneratorEnum;
import com.personal.gateway.flinkAllCommand.configurationOption.webObjectGenerator.WebObjectGeneratorInteger;
import com.personal.gateway.flinkAllCommand.configurationOption.webObjectGenerator.WebObjectGeneratorListMap;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.personal.gateway.custom.customEnum.flinkCommandEnum.FlinkExecutionEnum.EXECUTION_TYPE_CLASS;
import static com.personal.gateway.custom.customEnum.flinkCommandEnum.FlinkTargetEnum.TARGET_TYPE_CLASS;
import static com.personal.gateway.flinkAllCommand.flinkEnvironment.FlinkEnvironmentTypeClass.FLINK_ENVIRONMENT_TYPE_CLASS;


/**
 * @author BigHead
 * @date 2022/1/10 16:34
 * @description TestNewConfig
 */
public class TestNewConfig extends AbstractConfigurationTypeClass {

    private static final KeyIndexInfomationDao BASE_CONFIG_GROUP = new KeyIndexInfomationDao("基础配置", 1);
    private static final KeyIndexInfomationDao TARGET_CONFIG_GROUP = new KeyIndexInfomationDao("执行方式", 2);

    public static final KeyIndexInfomationDao ENVIRONMENT_GROUP = new KeyIndexInfomationDao("环境参数", 3);


    public final ConfigurationOption<Integer, ConfigurationTypeInteger> JM_MEMORY_PROCESS_SIZE = ConfigurationOptionBuilder
            .startBuilder(BASE_CONFIG_GROUP, new KeyIndexInfomationDao("jobmanager.memory.process.size", 1))
            .webShowName("JM的内存大小")
            .describe("JM的运行内存分配大小")
            .necessary(true)
            .typeInteger()
            .setExampleValue(2048)
            .webObjectGenerator(WebObjectGeneratorInteger.class)
            .parameterCheck(ParameterCheckInteger.class)
            .configurationGenerator(ConfigurationGeneratorInteger.class)
            .build();

    public final ConfigurationOption<Double, ConfigurationTypeDouble> TM_MEMORY_JVM_OVERHEAD_FRACTION = ConfigurationOptionBuilder
            .startBuilder(BASE_CONFIG_GROUP, new KeyIndexInfomationDao("taskmanager.memory.jvm-overhead.fraction", 2))
            .webShowName("TM的预留对外内存")
            .describe("预留给JVM开销的总进程内存的百分比。这是为JVM开销预留的堆外内存，比如线程堆栈空间、编译缓存等。这包括本机内存，但不包括直接内存，并且在Flink计算JVM max直接内存大小参数时不会被计算在内。JVM开销的大小用于构成已配置的总进程内存的部分。如果派生的大小小于/大于配置的min/max大小，则使用min/max大小。JVM开销的确切大小可以通过设置相同的最小/最大大小来显式指定")
            .necessary(false)
            .typeDouble()
            .setExampleValue(0.1)
            .setMinMaxValue(0.01, 1.0)
            .webObjectGenerator(WebObjectGeneratorDouble.class)
            .parameterCheck(ParameterCheckDouble.class)
            .configurationGenerator(ConfigurationGeneratorDouble.class)
            .build();

    public final ConfigurationOption<Enum<? extends ConfigurationTypeEnumInterface>, ConfigurationTypeEnum> executeEnum = ConfigurationOptionBuilder
            .startBuilder(BASE_CONFIG_GROUP, new KeyIndexInfomationDao("execute", 6))
            .webShowName("flink执行方式")
            .describe("执行方式")
            .necessary(true)
            .typeEnum(EXECUTION_TYPE_CLASS)
            .webObjectGenerator(WebObjectGeneratorEnum.class)
            .parameterCheck(ParameterCheckEnum.class)
            .configurationGenerator(ConfigurationGeneratorEnum.class)
            .build();

    public final ConfigurationOption<Enum<? extends ConfigurationTypeEnumInterface>, ConfigurationTypeEnum> targetEnum = ConfigurationOptionBuilder
            .startBuilder(TARGET_CONFIG_GROUP, new KeyIndexInfomationDao("target", 7))
            .webShowName("启动方式")
            .describe("启动方式")
            .necessary(true)
            .antecedentConfigurationOption(executeEnum)
            .typeEnum(TARGET_TYPE_CLASS)
            .webObjectGenerator(WebObjectGeneratorEnum.class)
            .parameterCheck(ParameterCheckEnum.class)
            .configurationGenerator(ConfigurationGeneratorEnum.class)
            .build();


    public final ConfigurationOption<AbstractConfigurationTypeClass, ConfigurationTypeClass> k8s_env = ConfigurationOptionBuilder
            .startBuilder(ENVIRONMENT_GROUP, new KeyIndexInfomationDao("abc", 8))
            .webShowName("abc")
            .describe("aabbcc")
            .necessary(true)
            .antecedentConfigurationOption(targetEnum)
            .typeClass(FLINK_ENVIRONMENT_TYPE_CLASS)
            .webObjectGenerator(null)
            .parameterCheck(null)
            .configurationGenerator(null)
            .build();

    public final ConfigurationOption<List<Map<String, String>>, ConfigurationTypeListMap> listMapValue = ConfigurationOptionBuilder
            .startBuilder(ENVIRONMENT_GROUP, new KeyIndexInfomationDao("kubernetes.jobmanager.owner.reference", 3))
            .webShowName("标签自动清理")
            .describe("用户指定的所有者引用被设置为JobManager部署。当所有的所有者资源被删除时，JobManager Deployment将被自动删除，同时也会删除由该Flink集群创建的所有资源。该值应该被格式化为一个以分号分隔的所有者引用列表，其中每个所有者引用都是一个以逗号分隔的‘key:value’对列表。")
            .necessary(true)
            .typeListMap()
            .setExampleValue(
                    Arrays.asList(new HashMap<String, String>() {{
                        put("a", "a");
                    }}, new HashMap<String, String>() {{
                        put("b", "b");
                    }}),
                    Arrays.asList(new HashMap<String, String>() {{
                        put("c", "c");
                        put("d", "d");
                    }}, new HashMap<String, String>() {{
                        put("e", "e");
                        put("f", "f");
                    }})
            )
            .webObjectGenerator(WebObjectGeneratorListMap.class)
            .parameterCheck(ParameterCheckListMap.class)
            .configurationGenerator(ConfigurationGeneratorListMap.class)
            .build();


    public static void main(String[] args) throws IllegalAccessException, InstantiationException {
        JSONObject webJson = new JSONObject();

        TestNewConfig testnewconfig = new TestNewConfig();
        
        System.out.println(testnewconfig.getWebElement(webJson));
        System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");

        webJson.put("execute", "run");
        System.out.println(testnewconfig.getWebElement(webJson));
        System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");

        webJson.put("target", "yarn-session");
        System.out.println(testnewconfig.getWebElement(webJson));
        System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
//        webJson.put("kubernetes000container000image", "yarn-session-test");
//        System.out.println(testnewconfig.getWebElement(webJson));
//        System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
//
//
//        webJson.put("target", "kubernetes-session");
//        System.out.println(testnewconfig.getWebElement(webJson));
//        System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
//        webJson.put("kubernetes000jobmanager000owner000reference", "p:p,o:o,i:i");
//        System.out.println(testnewconfig.getWebElement(webJson));
//        System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");


    }
}
